/* Author: smayo@google.com

*/
var ZOO = window.ZOO || {};

ZOO.Recog = function(){
  var pub = {
    recognizing: false,
    //final_transcript,
    recognition: null,


    init: function(){
      console.log('init');
      this.recognition = new webkitSpeechRecognition();
      this.recognition.continuous = true;
      this.recognition.interimResults = false;
      //this.reset();
      
      //events
      this.recognition.onend = $.proxy(this.reset, this);
      this.recognition.onresult = $.proxy(this.onRecogResult, this);
      this.toggleStartStop();

      return this;
    },
    onRecogResult: function(event){
      // Note: This early version uses result[], which improperly handles
      // interim values. Future versions will use results[] and resultIndex to
      // properly handle interim values.
      console.log(event);

      for (var i = event.resultIndex; i < event.results.length; ++i) {
        if (event.results[i].isFinal) {
          //final_transcript += event.results[i][0].transcript;
          console.log(event.results[i][0]);
          $(this).trigger('recogResult', event.results[i][0]);
        }
      }
    },
    reset: function() {
      console.log('speech recog ended :(');
      //TODO SRM - uncomment this
      this.recognition.start();
      this.recognizing = true;
    },
    toggleStartStop: function() {
      if (this.recognizing) {
        this.recognition.stop();
        this.reset();
      } else {
        this.recognition.start();
        this.recognizing = true;
        //button.innerHTML = "Click to Stop";
        //final_span.innerHTML = "";
        //interim_span.innerHTML = "";
      }
    },


  }
  return pub;
};


